<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin('',true);
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('qq','nickname','avatar','birth','pcd','mobile','realmobile','diyformdata'));
	if(!$member){
		exi('找不到会员信息！','error');
	}
	if($_SESSION['onemerchid']){
         model('jy_merch/member')->addmember(intval($_SESSION['onemerchid']));
    	 model('jy_merch/member')->up_addmember(intval($_SESSION['onemerchid']));
    	 model('jy_merch/member')->up_catefans(intval($_SESSION['onemerchid']));
    	 $return['ischecklogin']=model('jy_weishop/member')->checklogin('','','',intval($_SESSION['onemerchid']),true);
    }
	$item=pdo_get('jy_yiliao_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
	if($item['birth']){
		$item['birth']=json_decode($item['birth'],true);
	}else{
		$item['birth']=array('year'=>'1990','month'=>'10','day'=>'18');
	}
	$item['mobile']=$item['mobile']?:$member['mobile'];
	$item['oldmobile']=$member['mobile'];
	$setting=uni_setting('register');
	$item['login_type']=$setting['login_type'];
	$item['xsign'] = $_RQ['xsign'];
	$return['item']=$return['post']=$item;
	$return['areas']=pdo_getall('jy_yiliao_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'merchid'=>$_SESSION['onemerchid']),array('id','title'),'id');
	foreach($return['areas'] as &$sv){
		$sv['stores']=pdo_getall('jy_yiliao_store',array('area_id'=>$sv['id'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),array('id','title'),'id');
	}
	$return['years']=array();
	for($i=1914;$i<2100;$i++){
		$return['years'][]=$i;
	}
	$return['months']=array(1,2,3,4,5,6,7,8,9,10,11,12);
	$return['days']=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
	set_html('title','用户档案');
	
    $params=array(
    	'from_type'=>2,
    );
    $this->model('model')->checkuser(0,$params);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$params=array(
    	'from_type'=>2,
    );
    $this->model('model')->checkuser(0,$params);
	if($postdata['login_type']>0 && $postdata['oldmobile']!=$postdata['mobile'] && !check_sms_code($postdata['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($postdata['mobile']!=$oldmobile){
			exi('手机验证码错误！不能修改手机！','error');
		}
	}elseif($postdata['login_type']>0 && $postdata['oldmobile']!=$postdata['mobile']){
		$realmobile=true;
		$postdata['realmobile']=$postdata['mobile'];
	}
	if(strlen($postdata['mobile'])!=11 || $postdata['mobile'][0]!='1' || !is_numeric($postdata['mobile'])){
			exi('手机号码格式错误，请输入11位，1开头的手机号！！','error');
	}
	$ucount=pdo_count('core_members',array('mobile'=>trim($postdata['mobile']),'uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
	if($ucount){
		exi('手机号已存在，请更换!','error');
	}
	if(!$postdata['name']){
		exi('请先完善姓名！','error');
	}
	if(!$postdata['area_id']){
		exi('请选择区域','error');
	}
	if(!$postdata['storeid']){
		exi('请选择医院','error');
	}
	$postdata['pcd']=$postdata['pcd']?ijson_encode($postdata['pcd']):'{"广东","深圳","龙华"}';
	$fields=array('mobile','realmobile');
	$data=array_fields($fields,$postdata);
	pdo_update('core_members',$data,array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$_SESSION['member']['nickname']=$postdata['nickname'];
	$oldmobile=$oldmobile?:$postdata['oldmobile'];
	if($oldmobile!=$postdata['mobile'] && $realmobile){
		$mid=model('core/member')->merge_memberdata($postdata['mobile'],'',$oldmobile);
		if($_SESSION['uid']!=$mid){
			model('core/member')->login($mid);
		}
	}
	if($postdata['birth']){
		if($_SESSION['os']=='wxapp'){
			$postdata['birth']=explode('-',$postdata['birth']);
			$postdata['birth']=array(
				'year'=>$postdata['birth'][0],
				'month'=>$postdata['birth'][1],
				'day'=>$postdata['birth'][2],
			);
		}
	}
	$data=array_fields(array('name','username','password','mobile','area_id','storeid','sex','birth'),$postdata);
	$id=intval($postdata['id']);
	$data['birth']=ijson_encode($postdata['birth']);
	if($id){
		pdo_update('jy_yiliao_user',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_yiliao_user',$data);
	}
	//缺少jy_yiliao_user表数据
	$data =array(
		'name'=>$postdata['name'],
		'agetime'=>intval(strtotime($postdata['birth']['year'].'-'.$postdata['birth']['month'].'-'.$postdata['birth']['day'])),
		'sex'=>intval($postdata['sex']),
	);
	$isfa=pdo_get('jy_yiliao_body',array('isfa'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
	if($isfa['id']){
	    pdo_update('jy_yiliao_user',array('bodyid'=>$isfa['id']),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_yiliao_body',$data,array('id'=>$isfa['id'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
		
		pdo_update('jy_merch_member',array('hzid'=>$isfa['id'],'isdaan'=>1),array('mid'=>$isfa['mid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['merchid'] = $_SESSION['onemerchid'];
		$data['isfa']=1;
		$data['createtime']=time();
		pdo_insert('jy_yiliao_body',$data);
		$isfa=pdo_get('jy_yiliao_body',array('isfa'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
	    pdo_update('jy_yiliao_user',array('bodyid'=>$isfa['id']),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_yiliao_body',$data,array('id'=>$isfa['id'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
		pdo_update('jy_merch_member',array('hzid'=>$isfa['id'],'isdaan'=>1),array('mid'=>$isfa['mid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
	}
	if($postdata['xsign']>0){
	    
	    exi('设置成功！','','jy_yiliao/yyconfirm//merchid='.$_SESSION['onemerchid'].'&store_id='.$postdata['storeid']);
	    
	}else{
	    exi('设置成功！','','jy_merch/myshop//merchid='.$_SESSION['onemerchid']);
	}
}
if($_RQ['op']=='setmobile'){
	//获取手机号
	$post=get_postdata();
	$params=array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['container']=='ttapp'){
		$params['ttgid']=$_SESSION['appgid'];
	}elseif($_SESSION['container']=='aliapp'){
		$params['aligid']=$_SESSION['appgid'];
	}elseif($_SESSION['container']=='baiduapp'){
		$params['baidugid']=$_SESSION['appgid'];
	}else{
		$params['wxgid']=$_SESSION['appgid'];
	}
	$uid=pdo_getcolumn('core_fans',$params,'mid');
	if($uid){
		$mid=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$uid),'id');
	}
	//解密获取手机号		
	require ROOT_D.'/core/library/wxapp_decode/wxBizDataCrypt.php';
	if($_SESSION['container']=='ttapp'){
			$appidname='tt_appid';
	}elseif($_SESSION['container']=='aliapp'){
			$appidname='ali_appid';
	}elseif($_SESSION['container']=='baiduapp'){
			$appidname='baidu_appid';
	}else{
			$appidname='appid';
	}
	$appid=pdo_getcolumn('core_account_wxapp',array('id'=>$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']),$appidname);
	if(!$appid || !$_SESSION['session_key']){
			exi('小程序参数错误！无法获取粉丝信息！','error');
	}
	$pc = new WXBizDataCrypt($appid, $_SESSION['session_key']);
	$errCode = $pc->decryptData($post['encryptedData'], $post['iv'],$data);
	if ($errCode) {
			exi($errCode ,'error');
	}
	$data=json_decode($data,true);
	$mobile=$data['purePhoneNumber'];//获取没有区的手机号
	if(!$uid){
			//尚未注册，直接注册			
			$new_member=array(
				'mobile'=>$mobile,
				'realmobile'=>$mobile
			);
			$uid=model('core/member')->register($new_member);
			model('core/member')->login($uid);
			pdo_update('core_fans',array('mid'=>$uid),$params);
	}else{
			$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$uid),'mobile');
			if($mobile!=$oldmobile){
				pdo_update('core_members',array('mobile'=>$mobile,'realmobile'=>$mobile),array('uniacid'=>$_SESSION['uniacid'],'id'=>$uid));
				$mid=model('core/member')->merge_memberdata($mobile);
				if($_SESSION['uid']!=$mid){
					model('core/member')->login($mid);
				}
			}
	}
	$res=array('uid'=>$uid,'mobile'=>$mobile);
	exi($res);
}
if($_RQ['op']=='getdata'){
	$register=uni_setting('register');
	$register['user_xieyi']=$register['user_xieyi']?:'本平台尊重和保护用户的隐私。本隐私政策将告诉您我们如何收集和使用有关您的信息，以及我们如何保护这些信息的安全。您成为本平台用户前务必仔细阅读本隐私条款并同意所有隐私条款。本隐私政策条款在您注册成为本平台用户后立即生效，并对您及本平台产生约束力。';
	$return['register']=$register;
	exi($return);
}
if($_RQ['op']=='postmobile'){
	$postdata=get_postdata();
	if($postdata['check']!=1){
			exi('请先阅读协议！','error');
	}
	if($postdata['login_type']>0 && !check_sms_code($postdata['sms_code'])){
			exi('手机验证码错误！','error');
	}
	if(strlen($postdata['mobile'])!=11 || $postdata['mobile'][0]!='1' || !is_numeric($postdata['mobile'])){
			exi('手机号码格式错误，请输入11位，1开头的手机号！！','error');
	}
	if($postdata['login_type']>0){
		$realmobile=true;
		$postdata['realmobile']=$postdata['mobile'];
	}
	$fields=array('mobile','realmobile');
	$data=array_fields($fields,$postdata);
	$_SESSION['member']['mobile']=$postdata['mobile'];
	pdo_update('core_members',$data,array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($realmobile){
		$mid=model('core/member')->merge_memberdata($postdata['mobile']);
		if($_SESSION['uid']!=$mid){
			model('core/member')->login($mid);
			exi('设置成功！登录已绑定本手机的会员！');
		}
	}
	exi('设置成功！');
}